package com.geng.invoker;

import com.alibaba.fastjson.JSON;
import com.geng.dto.MyResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class NettyClientReceiveMsgHandler extends SimpleChannelInboundHandler<MyResponse> {

    public NettyClientReceiveMsgHandler() {
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MyResponse myResponse) throws Exception {
        //将Netty异步返回的结果存入阻塞队列,以便调用端同步获取
       // System.out.println("返回结果" + JSON.toJSONString(myResponse));
        InvokerResponseHolder.putResultValue(myResponse);
    }
}
